# -*- coding: utf-8 -*-
"""
Created on 2021-09-14 13:34:55
---------
@summary:
---------
@author: xubin
"""

import feapder
from feapder.utils.tools import get_md5
from feapder import Item


class CreateTaskForCloudClone(feapder.AirSpider):
    __custom_setting__ = dict(
        MYSQL_IP="192.168.1.99",
        MYSQL_PORT=3307,
        MYSQL_DB="feapder",
        MYSQL_USER_NAME="root",
        MYSQL_USER_PASS="root",
    )

    def start_requests(self):
        yield feapder.Request("http://www.cloud-clone.com.cn/diseaseModels.html")

    def parse(self, request, response):
        ul_nodes = response.xpath('//ul[@class="search-show cate-show"]')
        for node in ul_nodes:
            title_list = [_.get() for _ in node.xpath('.//li/a/text()')]
            url_list = [_.get() for _ in node.xpath('.//li/a/@href')]
            dic = dict(map(lambda x, y: [x, y], title_list, url_list))

            for k, v in dic.items():
                item = Item()
                item.table_name = "clone_task_info"
                item.id = get_md5(response.url)
                item.name = k
                item.url = v
                item.create_by = 'xubin'
                yield item


class CreateTaskForUSCnk(feapder.AirSpider):
    __custom_setting__ = dict(
        MYSQL_IP="192.168.1.99",
        MYSQL_PORT=3307,
        MYSQL_DB="feapder",
        MYSQL_USER_NAME="root",
        MYSQL_USER_PASS="root",
    )

    def start_requests(self):
        yield feapder.Request("http://www.uscnk.cn/Search/search.htm?keywords=&type=5&specie=")

    def parse(self, request, response):
        ul_nodes = response.xpath('//ul[@class="cate"]')
        for node in ul_nodes:
            title_list = [_.get() for _ in node.xpath('.//li/a/text()')]
            url_list = [_.get() for _ in node.xpath('.//li/a/@href')]

            dic = dict(map(lambda x, y: [x, y], title_list, url_list))
            for k, v in dic.items():
                # item = Item()
                # item.table_name = "clone_uscnk_task_info"
                # item.id = get_md5(response.url)
                # item.name = k
                # item.url = v
                # item.create_by = 'xubin'
                # yield item
                yield feapder.Request(v, callback=self.second_parse, k=k)

    def second_parse(self, request, response):
        class_name = request.k
        ul_nodes = response.xpath('//ul[@class="specie"]')
        for node in ul_nodes:
            title_list = [_.get() for _ in node.xpath('.//li/a/text()')]
            url_list = [_.get() for _ in node.xpath('.//li/a/@href')]
            dic = dict(map(lambda x, y: [x, y], title_list, url_list))

            for k, v in dic.items():
                item = Item()
                item.table_name = "clone_uscnk_task_info"
                item.id = get_md5(v)
                item.name = class_name
                item.url = v
                item.sub_screen = k
                item.create_by = 'xubin'
                yield item


if __name__ == "__main__":
    # CreateTaskForCloudClone().start()
    CreateTaskForUSCnk().start()
